﻿
var DATA_NOT_FOUND = "DATA_NOT_FOUND";
var DATA_NULL = "DATA_NULL";
var VERSION_PREFIX = "V_";

var FCC_GetValueFromCache = function (key, version, callback) {
    __FCC_GetValueFromCacheInner(key, VERSION_PREFIX + version, (s, d, e) => {

        if (s) {
            callback(s, d, e);
        } else {
            __FCC_GetValueFromCacheInner(key, version, callback);
        }

    });
};

var __FCC_GetValueFromCacheInner = function (key, version, callback) {

    if (window.localKv && window.localKv.retrieve2) {
        // HAC
        value = window.localKv.retrieveV2(key, version);

        if (value !== DATA_NOT_FOUND) {

            try {
                value = JSON.parse(value);
                callback(true, value);
            } catch (error) {
                var errorMsg = "在离线缓存读取的值执行反序列化时出错：" + errorMsg + "，原始数据为：" + value;
                callback(false, "", errorMsg);
            }
        } else {
            var errorMsg = "在离线缓存中没有找到指定的键：" + key + "，版本：" + version;
            callback(false, "", errorMsg);
        }

    } else {
        // 浏览器
        var realKey = key;
        var valueB = JSON.parse(localStorage.getItem(realKey));

        if (valueB && valueB.version === version && valueB.value) {

            if (valueB.value === undefined) {
                var errorMsg = "在离线缓存中没有找到指定的键：" + key + "，版本：" + version;
                callback(false, "", errorMsg);
            } else if (valueB.value === DATA_NULL) {
                value = null;
                callback(true, value);
            } else {
                value = valueB.value;
                callback(true, value);
            }
        } else {
            var errorMsg = "在离线缓存中没有找到指定的键：" + key + "，版本：" + version;
            callback(false, "", errorMsg);
        }
    }

};

var Upsert_LocalCache_Command = (function (_super) {
    __extends(Upsert_LocalCache_Command, _super);
    function Upsert_LocalCache_Command() {
        return _super !== null && _super.apply(this, arguments) || this;
    }

    Upsert_LocalCache_Command.prototype.execute = function () {

        var param = this.CommandParam;
        var key = this.evaluateFormula(param.KeyString);
        var value = this.evaluateFormula(param.ValueString);
        var version = this.evaluateFormula(param.VersionString);

        version = VERSION_PREFIX + version;

        if (value === DATA_NOT_FOUND || value === DATA_NULL) {
            throw "不能传入本插件用于标示没找到缓存的关键字：DATA_NOT_FOUND、DATA_NULL";
        } else {

            if (window.localKv) {

                if (value === null) {
                    value = DATA_NULL;
                }

                if (value instanceof Object) {
                    value = JSON.stringify(value);
                }

                window.localKv.upsertV(key, value, version);
            } else {
                var realKey = key;
                localStorage.setItem(realKey, JSON.stringify({
                    version: version,
                    value: value
                }));
            }
        }
    };

    return Upsert_LocalCache_Command;
}(Forguncy.CommandBase));

// Key format is "Namespace.ClassName, AssemblyName"
Forguncy.CommandFactory.registerCommand("FrontendCacheCommand.Upsert_LocalCache, FrontendCacheCommand", Upsert_LocalCache_Command);


var Retrieve_LocalCache_Command = (function (_super) {
    __extends(Retrieve_LocalCache_Command, _super);
    function Retrieve_LocalCache_Command() {
        return _super !== null && _super.apply(this, arguments) || this;
    }

    Retrieve_LocalCache_Command.prototype.execute = function () {

        var param = this.CommandParam;
        var key = this.evaluateFormula(param.KeyString);
        var version = this.evaluateFormula(param.VersionString);
        var targetCellFormulaV = param.TargetCell;
        var cellLocationV = this.getCellLocation(targetCellFormulaV);
        var value = DATA_NOT_FOUND;

        FCC_GetValueFromCache(key, version, (s, d, e) => {
            if (s) {
                value = d;
            } else {
                console.warn(e);
            }

            Forguncy.Page.getCellByLocation(cellLocationV).setValue(value);
        });

    };

    return Retrieve_LocalCache_Command;
}(Forguncy.CommandBase));

// Key format is "Namespace.ClassName, AssemblyName"
Forguncy.CommandFactory.registerCommand("FrontendCacheCommand.Retrieve_LocalCache, FrontendCacheCommand", Retrieve_LocalCache_Command);

var Reset_LocalCache_Command = (function (_super) {
    __extends(Reset_LocalCache_Command, _super);
    function Reset_LocalCache_Command() {
        return _super !== null && _super.apply(this, arguments) || this;
    }

    Reset_LocalCache_Command.prototype.execute = function () {

        var param = this.CommandParam;
        var key = this.evaluateFormula(param.KeyString);

        if (window.localKv) {
            // HAC
            window.localKv.remove(key);
        } else {
            // 浏览器
            localStorage.removeItem(key);
        }

    };

    return Reset_LocalCache_Command;
}(Forguncy.CommandBase));

// Key format is "Namespace.ClassName, AssemblyName"
Forguncy.CommandFactory.registerCommand("FrontendCacheCommand.Reset_LocalCache, FrontendCacheCommand", Reset_LocalCache_Command);

var Retrieve_LocalCache2_Command = (function (_super) {
    __extends(Retrieve_LocalCache2_Command, _super);
    function Retrieve_LocalCache2_Command() {
        return _super !== null && _super.apply(this, arguments) || this;
    }

    Retrieve_LocalCache2_Command.prototype.execute = function () {

        var param = this.CommandParam;
        var key = this.evaluateFormula(param.KeyString);
        var version = this.evaluateFormula(param.VersionString);
        var OutParamaterName = param.OutParamaterName;
        var value = DATA_NOT_FOUND;

        FCC_GetValueFromCache(key, version, (s, d, e) => {
            if (s) {
                value = d;
            } else {
                console.warn(e);
            }

            if (OutParamaterName && OutParamaterName != "") {
                Forguncy.CommandHelper.setVariableValue(OutParamaterName, value);
            } else {
                this.log("OutParamaterName was not set, the value is: " + JSON.stringify(value));
            }
        });

    };

    return Retrieve_LocalCache2_Command;
}(Forguncy.CommandBase));

// Key format is "Namespace.ClassName, AssemblyName"
Forguncy.CommandFactory.registerCommand("FrontendCacheCommand.Retrieve_LocalCache2, FrontendCacheCommand", Retrieve_LocalCache2_Command);

var Retrieve_LocalCache3_Command = (function (_super) {
    __extends(Retrieve_LocalCache3_Command, _super);
    function Retrieve_LocalCache3_Command() {
        return _super !== null && _super.apply(this, arguments) || this;
    }

    Retrieve_LocalCache3_Command.prototype.execute = function () {
        var me = this;
        var param = this.CommandParam;
        var key = this.evaluateFormula(param.KeyString);
        var version = this.evaluateFormula(param.VersionString);


        FCC_GetValueFromCache(key, version, (s, d, e) => {
            me.CommandExecutor.excuteCommand(me.CommandParam.CommandList, {
                runTimePageName: me.CommandExecutingInfo.runTimePageName,
                commandID: new Date().valueOf().toString(),
                initParams: {
                    "Value": d,
                    "IsSuccess": s,
                    "Error": e
                },
                locationString: "执行读取离线缓存值的回调"
            });
        });
    };

    return Retrieve_LocalCache3_Command;
}(Forguncy.CommandBase));

// Key format is "Namespace.ClassName, AssemblyName"
Forguncy.CommandFactory.registerCommand("FrontendCacheCommand.Retrieve_LocalCache3, FrontendCacheCommand", Retrieve_LocalCache3_Command);

